package ead.cs.web.controller;

import ead.cs.domain.Coordinates;
import ead.cs.domain.Payment;
import ead.cs.domain.Shipment;
import ead.cs.service.PaymentService;
import ead.cs.service.ShipScheduleService;
import ead.cs.service.ShipService;
import ead.cs.service.ShipmentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *
 * @author cjayawickrema
 */
@Controller
@RequestMapping(value = "/track/")
public class TrackingController {

    private static final Logger LOGGER = Logger.getLogger(TrackingController.class);

    @Autowired
    private ShipService shipService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private ShipScheduleService shipScheduleService;
    @Autowired
    private ShipmentService shipmentService;


    @RequestMapping(method = RequestMethod.GET, value = "/shipment/{paymentId}")
    @ResponseBody
    public Coordinates getShipmentCoordinates(@PathVariable String paymentId) {
//        return new Coordinates(37.772323, -122.214897);
        Shipment shipment = shipmentService.findByPaymentId(paymentId);
        return shipment.getPosition();


    }

    @RequestMapping(method = RequestMethod.GET, value = "/route/{paymentId}")
    @ResponseBody
    public List<Coordinates> getRouteCoordinates(@PathVariable String paymentId) {
        Shipment shipment = shipmentService.findByPaymentId(paymentId);
        return shipment.getRoute();

//        return new Coordinates[]{
//            new Coordinates(37.772323, -122.214897),
//            new Coordinates(21.291982, -157.821856),
//            new Coordinates(-33.7969235, 150.9224326)
//        };
    }

    @RequestMapping(method = RequestMethod.GET, value = "/ship/{shipmentReference}")
    @ResponseBody
    public Payment getShipDetails(@PathVariable String shipmentReference) {
        return paymentService.findOne(shipmentReference);
    }

}
